(function() {
	var _typesname = "yymob"; //名字可改
	var allsource; //当前上下文绑定数据

	var temData = { //运行临时数据
		ArrayHtmlTemp: [] //数组绑定模板
	};

	function replaceAll(stringAll, str1, str2) {
		while(1 == 1) {
			if(stringAll.indexOf(str1) == -1) {
				return stringAll;
			}
			stringAll = stringAll.replace(str1, str2);
		}
	}

	var yymob = function(selecter) {
		return document.querySelectorAll(selecter);
	};

	yymob.math = {
		/**
		 * 加
		 */
		accAdd: function(arg1, arg2) {
			try {

				arg1 = arg1 || 0.00;
				arg2 = arg2 || 0.00;

				var r1, r2, m, c;

				try {
					r1 = arg1.toString().split(".")[1].length;
				} catch(e) {
					r1 = 0;
				}

				try {
					r2 = arg2.toString().split(".")[1].length;
				} catch(e) {
					r2 = 0;
				}

				c = Math.abs(r1 - r2);
				m = Math.pow(10, Math.max(r1, r2));
				if(c > 0) {
					var cm = Math.pow(10, c);
					if(r1 > r2) {
						arg1 = Number(arg1.toString().replace(".", ""));
						arg2 = Number(arg2.toString().replace(".", "")) * cm;
					} else {
						arg1 = Number(arg1.toString().replace(".", "")) * cm;
						arg2 = Number(arg2.toString().replace(".", ""));
					}
				} else {
					arg1 = Number(arg1.toString().replace(".", ""));
					arg2 = Number(arg2.toString().replace(".", ""));
				}
				return Number(((arg1 + arg2) / m).toFixed(2));
			} catch(e) {}

		},

		/**
		 * 减
		 */
		accMinus: function(arg1, arg2) {
			try {
				arg1 = arg1 || 0.00;
				arg2 = arg2 || 0.00;
				return com.yonyou.eshengbuyerapp.comm.accAdd(arg1, -1 * arg2);
			} catch(e) {}

		},

		/**
		 * 乘
		 */
		accMul: function(num1, num2) {
			try {
				num1 = num1 || 0.00;
				num2 = num2 || 0.00;
				var len = 0,
					str1 = num1.toString(),
					str2 = num2.toString();
				if(str1.indexOf('.') >= 0) {
					len += str1.split('.')[1].length;
				}
				if(str2.indexOf('.') >= 0) {
					len += str2.split('.')[1].length;
				}

				var result = Number(str1.replace('.', '')) * Number(str2.replace('.', '')) / Math.pow(10, len);
				return result.toFixed(2);
			} catch(e) {}
		},

		/**
		 * 除
		 */
		accDiv: function(num1, num2) {

			try {
				num1 = num1 || 0.00;
				num2 = num2 || 0.00;
				if(num2 == 0) {
					return 0.00;
				}
				var len1 = 0,
					len2 = 0,
					r1, r2;
				var result = 0;
				var str1 = num1.toString(),
					str2 = num2.toString();
				//计算位数差
				if(str1.indexOf('.') > -1) {
					len1 = str1.split('.')[1].length;
				}
				if(str2.indexOf('.') > -1) {
					len2 = str2.split('.')[1].length;
				}
				with(Math) {
					r1 = Number(num1.toString().replace(".", ""))
					r2 = Number(num2.toString().replace(".", ""))
					result = (r1 / r2) * pow(10, len2 - len1);
				}
				return result.toFixed(2);
			} catch(e) {}
		},
	};
	yymob.win = {
		OpenWin: function(PageUrl, jsons) {
			if(jsons == null) {
				localStorage.yymob_no_use_Para = "";
			} else {
				localStorage.yymob_no_use_Para = JSON.stringify(jsons);
			}
			window.location.href = PageUrl;
		},
		GetPara: function() {
			var aa = localStorage.yymob_no_use_Para;
			if(aa) {
				return JSON.parse(aa);
			}
			return null;
		}
	};
	yymob.source = {
		bind: function(jsons) {
			if(jsons == null) {
				jsons = allsource;
			}
			//更新上下文菜單
			if(!allsource) {
				allsource = jsons;
			} else {
				for(var i in jsons) {
					allsource[i] = jsons[i];
				}
			}
			//绑定
			var objs;
			for(var i in jsons) {
				if(jsons[i] instanceof Array) {
					//绑定表格
					try {
						objs = document.querySelectorAll("[yybindid='" + i + "']");
						for(var j = 0; j < objs.length; j++) { //所有表格
							var elements = document.createElement(objs[j].tagName); //当前表格
							var elementCode = objs[j].getAttribute("yytempid");
							if(!elementCode) {
								elementCode = new Date().getTime().toString();
								temData.ArrayHtmlTemp[elementCode] = objs[j].innerHTML;
								objs[j].setAttribute("yytempid", elementCode);
							}
							var htmls = ""; //最后的html

							for(var k = 0; k < jsons[i].length; k++) { //循环数组
								elements.innerHTML = temData.ArrayHtmlTemp[elementCode];
								var arr = (elements.innerHTML.match(/\{\%\s*.*?\s*\%\}/g) || []).map(function(text) {
									return text.substring(2, text.length - 2);
								})
								for(var k1 in jsons[i][k]) { //循环数组元素
									for(var k2 = 0; k2 < arr.length; k2++) { //循环{% %}数组
										if(arr[k2].indexOf(k1) != -1) {
											var ons = arr[k2].replace(eval("/" + k1 + "/g"), "'" + jsons[i][k][k1] + "'");
											elements.innerHTML = replaceAll(elements.innerHTML, "{%" + arr[k2] + "%}", "{%" + ons + "%}");
										}
									}
								}
								//重新获取
								arr = (elements.innerHTML.match(/\{\%\s*.*?\s*\%\}/g) || []).map(function(text) {
									return text.substring(2, text.length - 2);
								})
								for(var k2 = 0; k2 < arr.length; k2++) { //循环{% %}数组
									try {
										elements.innerHTML = replaceAll(elements.innerHTML, "{%" + arr[k2] + "%}", eval(arr[k2]));
									} catch(e) {
										console.log("绑定列表转换时出错！提示绑定值均为字符串！请检查是否有语法错误!");
									}
								}

								htmls += elements.innerHTML;
							}
							objs[j].innerHTML = htmls;
						}
					} catch(e) {}
				} else {
					//绑定一般数据
					try {
						objs = document.querySelectorAll("[yybindid='" + i + "']");
						for(var j in objs) {
							objs[j].oninput = function() {
								var o = this.getAttribute("yybindid");
								if(allsource[o]) {
									allsource[o] = this.value;
								}
							};
							objs[j].checked = jsons[i];
							objs[j].value = jsons[i];
							objs[j].innerText = jsons[i];

						}
					} catch(e) {}
				}

			}
		},
		//传入绑定字段 获取值
		GetAll: function(ob) {
			if(ob) {
				return allsource[ob];
			} else {
				return allsource;
			}
		},
	}

	yymob.ChangeName = function(na) {
		eval("window." + na + "=yymob");
	};
	yymob.ReadCache = function(name) {
		return eval("localStorage." + name);
	};
	yymob.WriteCache = function(name, value) {
		eval("localStorage." + name + " = " + value + ";");
	};
	yymob.JsonToString = function(va) {
		try {
			return JSON.stringify(va);
		} catch(e) {
			console.log("JsonToString转化时出错:" + e);
		}
	};
	yymob.StringToJson = function(va) {
		return JSON.parse(data + "");
	};
	//获取设备信息
	yymob.UserDriver = function() {
		var ua = navigator.userAgent.toLowerCase();
		if(/iphone|ipad|ipod/.test(ua)) {
			return "ios";
		} else if(/android/.test(ua)) {
			return "android";
		} else {
			return "pc";
		}
	};
	yymob.ChangeName(_typesname);

	//html,body{margin:0; height: 100%;}
	yymob.reHeight = function(){
		var cou = 0; 
		while(cou < 2){
			cou++;
			
			var obj = new Array();
			var objs = document.querySelectorAll("[yyheight]");
			for(var i = 0;i<objs.length;i++){
				if(obj.indexOf(objs[i].parentElement) == -1){
					obj.push(objs[i].parentElement);
				}
			}
			for(var i=0;i<obj.length;i++){//父节点
				var meheight = Number(obj[i].offsetHeight);//剩余高度
				var allcount = 0;//所有比重
				
				//父容器padding
				meheight -= Number(_getStyle(obj[i],"paddingTop").replace("px",""));
				meheight -= Number(_getStyle(obj[i],"paddingBottom").replace("px",""));
				meheight -=2;//防止出现滚动条
		
				var childnods = obj[i].children;
				//计算总比重
				var lastnodesBottom;//上一个节点下外编剧
				for(var j =0;j<childnods.length;j++){
					var a = childnods[j].getAttribute("yyheight");
					if(!!a){
						allcount += Number(a);
					}else{
						meheight -= childnods[j].offsetHeight;
					}
					//边距删除
					var v1 = Number(_getStyle(childnods[j],"marginTop").replace("px",""));
					var v2 = Number(_getStyle(childnods[j],"marginBottom").replace("px",""));
					if(j == 0){
						meheight -= v1;
					}else{
						if(v1 > lastnodesBottom){
							meheight -= v1;
						}else{
							meheight -= lastnodesBottom;
						}
					}
					if(j == childnods.length-1){
						meheight -= v2;
					}
					lastnodesBottom = v2;
				}
				//更新高度
				for(var j =0;j<childnods.length;j++){
					var a = childnods[j].getAttribute("yyheight");
					if(!!a){
						childnods[j].style.height = (meheight/allcount*Number(a)).toString() + "px";
					}
				}
			}
		}
	}
	// 只能分别获取 width、marginTop、paddingTop等, 这样获取效率太低,有没方法可以一次获取得到呢?
	function _getStyle(obj,attr){ 
	  if(obj.currentStyle){ 
	    return obj.currentStyle[attr]; 
	  } 
	  else{ 
	    return document.defaultView.getComputedStyle(obj,null)[attr]; 
	  } 
	}

	window.onload = function(){
		yymob.reHeight();
	}
		
})()
